package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.ReceivedMessages;
import com.example.mall.dto.ReceivedMessagesDto;
import com.example.mall.mapper.ReceivedMessagesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ReceivedMessagesQuery;
import com.example.mall.service.IReceivedMessagesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 接收消息表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class ReceivedMessagesServiceImpl extends ServiceImpl<ReceivedMessagesMapper, ReceivedMessages> implements IReceivedMessagesService {

    @Autowired
    private ReceivedMessagesMapper receivedMessagesMapper;

    @Override
    public PageDTO<ReceivedMessagesDto> queryPage(ReceivedMessagesQuery receivedMessagesQuery) {
        if (receivedMessagesQuery == null) {
            receivedMessagesQuery = new ReceivedMessagesQuery();
        }
        Page<ReceivedMessages> page = receivedMessagesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ReceivedMessages> cartPage = lambdaQuery()
                .like(receivedMessagesQuery.getReceivedId()!= null, ReceivedMessages::getReceivedId, receivedMessagesQuery.getReceivedId())
                .like(receivedMessagesQuery.getMessageId()!= null, ReceivedMessages::getMessageId, receivedMessagesQuery.getMessageId())
                .like(receivedMessagesQuery.getTopic()!= null, ReceivedMessages::getTopic, receivedMessagesQuery.getTopic())
                .like(receivedMessagesQuery.getTag()!= null, ReceivedMessages::getTag, receivedMessagesQuery.getTag())
                .like(receivedMessagesQuery.getBody()!= null, ReceivedMessages::getBody, receivedMessagesQuery.getBody())
                .like(receivedMessagesQuery.getReceiveStatus()!= null, ReceivedMessages::getReceiveStatus, receivedMessagesQuery.getReceiveStatus())
                .like(receivedMessagesQuery.getUserId()!= null, ReceivedMessages::getUserId, receivedMessagesQuery.getUserId())
                .like(receivedMessagesQuery.getOrderId()!= null, ReceivedMessages::getOrderId, receivedMessagesQuery.getOrderId())
                .like(receivedMessagesQuery.getDescription()!= null, ReceivedMessages::getDescription, receivedMessagesQuery.getDescription())
                .apply(receivedMessagesQuery.getReceiveTime() != null,
                        "DATE_FORMAT(receive_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        receivedMessagesQuery.getReceiveTime())
                .orderByDesc(ReceivedMessages::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ReceivedMessagesDto.class);
    }

    @Override
    public ReceivedMessages queryReceivedMessagesById(Long receivedId) {
        AssertUtil.isNotNull(receivedId, "receivedId不能为空");
        return receivedMessagesMapper.selectById(receivedId);
    }

    @Override
    public List<ReceivedMessages> queryReceivedMessagesList() {
        return this.list();
    }

    @Override
    public void insert(ReceivedMessagesDto receivedMessagesDto) {
        ReceivedMessages receivedMessages = new ReceivedMessages();
        CopyUtils.copyProperties(receivedMessagesDto, receivedMessages);
        receivedMessages.setReceivedId(CodeGenerateUtils.generateUniqueId());
        receivedMessages.setCreateDate(new Date());
        receivedMessages.setUpdateDate(new Date());
        receivedMessages.setReceiveStatus(BaseConstants.NOT_USED);
        receivedMessagesMapper.insert(receivedMessages);
    }

    @Override
    public void update(ReceivedMessagesDto receivedMessagesDto) {
        AssertUtil.isNotNull(receivedMessagesDto.getReceivedId(), "receivedId不能为空");
        ReceivedMessages receivedMessages = receivedMessagesMapper.selectById(receivedMessagesDto.getReceivedId());
        AssertUtil.isNotNull(receivedMessages, "该记录不存在");
        CopyUtils.copyProperties(receivedMessagesDto, receivedMessages);
        receivedMessages.setUpdateDate(new Date());
        receivedMessagesMapper.updateById(receivedMessages);
    }

    @Override
    public void delete(Long receivedId) {
        AssertUtil.isNotNull(receivedId, "receivedId不能为空");
        ReceivedMessages receivedMessages = receivedMessagesMapper.selectById(receivedId);
        AssertUtil.isNotNull(receivedMessages, "该记录不存在");
        receivedMessages.setReceiveStatus(BaseConstants.EXPIRED);
        receivedMessages.setUpdateDate(new Date());
        receivedMessagesMapper.updateById(receivedMessages);
    }

    @Override
    public void deleteBatch(List<Long> receivedIds) {
        AssertUtil.isNotNull(receivedIds, "receivedIds不能为空");
        List<ReceivedMessages> receivedMessages = receivedMessagesMapper.selectBatchIds(receivedIds);
        AssertUtil.isNotNull(receivedMessages, "该记录不存在");
        for (ReceivedMessages receivedMessage : receivedMessages) {
            receivedMessage.setReceiveStatus(BaseConstants.EXPIRED);
            receivedMessage.setUpdateDate(new Date());
            receivedMessagesMapper.updateById(receivedMessage);
        }
    }
}
